<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text" id='inputDom'>
    <span id='text'></span>

    <script>
        window.onload = function () {
            let data = {
                msg: '默认值'
            }
            let tempdata = JSON.parse(JSON.stringify(data)) //拷贝解决死循环
            Object.defineProperty(data, 'msg', {
                set(value) {
                    tempdata.msg = value
                    render()
                }
            })
            //视图渲染
            function render() {
                inputDom.value = tempdata.msg
                text.innerHTML = tempdata.msg
            }
            render()
            //oninput
            inputDom.addEventListener('input', function () {
                data.msg = this.value
            })
        }
    </script>
</body>

</html>